package com.project.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.project.model.CityModel;
import com.project.model.RankingModel;
import com.project.service.CityService;
import com.project.service.RedPacketService;

@Controller
@RequestMapping(value = "/ranking")
public class RankingController {
	private static Logger logger = LoggerFactory.getLogger(RankingController.class);
	
	@Autowired
	private RedPacketService redPacketService;
	
	@Autowired
	private CityService cityService;
	
	@RequestMapping("")
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String provinceCode = "110000";
		String cityCode = "110100";
		if (null != request.getParameter("province") && !"".equals(request.getParameter("province").trim())) {
			String province = request.getParameter("province").trim();
			List<CityModel> provinces = cityService.getProvinceCodeByName(province);
			if (null != provinces && provinces.size() > 0) {
				provinceCode = provinces.get(0).getCode();
			}
		}
		if (null != request.getParameter("city") && !"".equals(request.getParameter("city").trim())) {
			String city = request.getParameter("city").trim();
			List<CityModel> citys = cityService.getCityCodeByName(city);
			if (null != citys && citys.size() > 0) {
				cityCode = citys.get(0).getCode();
			}
		}
		Map<String, Object> condition = new HashedMap<String, Object>();
		condition.put("limit", 4);
		condition.put("province", provinceCode);
		condition.put("city", cityCode);
		List<RankingModel> list = redPacketService.getRankingList(condition);
		if (null == list || list.size() == 0) {
			list = new ArrayList<RankingModel>();
		}
		mav.addObject("rankings", list);
		mav.setViewName("/ranking.jsp");
		return mav;
	}
}